/**  
* Copyright (c) 2009, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// created by noellynch
// Sep 28, 2009

package com.sceneworker.editors.texturepainter.brushgenerator;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

import javax.swing.JComponent;

public class BrushImage  extends JComponent {

	private		BufferedImage				mcl_image;
	private		IQuadraticBrushSpectrum		mcl_spectrum;
	private		float						mfa_intensityValues[];
	private		int							mi_curSpectrumControlValue = Integer.MAX_VALUE;
	private		float						mf_strength = 1.0f;
	
	public	BrushImage(IQuadraticBrushSpectrum cl_specturm) {
		mcl_spectrum = cl_specturm;
		mcl_image = new BufferedImage(mcl_spectrum.getSamples() << 1, mcl_spectrum.getSamples() << 1, BufferedImage.TYPE_INT_ARGB);
		initIntensityValues();
	}
	
	public	void	recreateBrush() {
		mcl_image = new BufferedImage(mcl_spectrum.getSamples() << 1, mcl_spectrum.getSamples() << 1, BufferedImage.TYPE_INT_ARGB);
		initIntensityValues();
		repaint();
	}
	
	public	void	setStrength(float f_strength) {
		mf_strength = f_strength;
		refreshIntensityValues();
	}
	
	@Override
	protected void paintComponent(Graphics cl_g) {
		Graphics2D		lcl_g = mcl_image.createGraphics();
		
		BrushImagePaint		lcl_p = new BrushImagePaint(mcl_spectrum);
		lcl_g.setPaint(lcl_p);
		lcl_g.fillRect(0, 0, mcl_image.getWidth(), mcl_image.getHeight());
		lcl_g.dispose();
		
		cl_g.drawImage(mcl_image, (getWidth() >> 1) - (mcl_image.getWidth() >> 1), (getHeight() >> 1) - (mcl_image.getHeight() >> 1), this);
		
		if((int)mcl_spectrum.getControlPoint().getX() != mi_curSpectrumControlValue) {
			//System.out.println("refreshing form paint");
			refreshIntensityValues();
			mi_curSpectrumControlValue = (int)mcl_spectrum.getControlPoint().getX();
		}
		
	}
	
	public	BufferedImage		getBrushImage() {
		return mcl_image;
	}

	@Override
	public Dimension getMaximumSize() {
		return new Dimension(85, 85);
	}

	@Override
	public Dimension getMinimumSize() {
		return getMaximumSize();
	}

	@Override
	public Dimension getPreferredSize() {
		return getMaximumSize();
	}
	
	public	float[]	getIntensityValues() {
		if(mfa_intensityValues == null) {
			initIntensityValues();
		}
		
		return mfa_intensityValues;
	}
	
	public	int	getBrushRadius() {
		return mcl_spectrum.getSamples();
	}
	
	public	void	refreshIntensityValues() {
		WritableRaster		lcl_r = mcl_image.getRaster();
		int					lia_pixels[] = new int[mcl_image.getWidth() * mcl_image.getHeight() * 4];
		lcl_r.getPixels(0, 0, mcl_image.getWidth(), mcl_image.getHeight(), lia_pixels);
		
		for(int li_i = 0; li_i < lia_pixels.length; li_i += 4) {
			mfa_intensityValues[li_i / 4] = (1f - ((float)lia_pixels[li_i] / 255f)) * mf_strength;
		}
		
		/*String	lstr_temp;
		for(int li_y = 0; li_y < mcl_image.getHeight(); li_y++) {
			lstr_temp = "";
			for(int li_x = 0; li_x < mcl_image.getWidth(); li_x++) {
				lstr_temp += String.format("%.2f ", mfa_intensityValues[li_x + (li_y * mcl_image.getWidth())]);
			}
			
			System.out.println(lstr_temp);
		}*/
	}
	
	private	void	initIntensityValues() {
		mfa_intensityValues = new float[mcl_image.getWidth() * mcl_image.getHeight()];
	}
}
